# ---
# title: "Negotiating with your mouth full (modelling code)"
# in support of paper: "Negotiating with your mouth full: Intergovernmental negotiations between transparency and confidentiality"
# paper authors: "Mareike Kleine and Samuel Huntington"
# code point of contact: "Samuel Huntington"
# date: "16 September, 2024"
# ---

# libraries
library(janitor)
library(tidyr)
library(pscl)
library(dplyr)
library(ggplot2)
library(readxl)
library(lubridate)
library(readr)
library(tseries)
library(haven)
library(writexl)


# ingest (pre-processed) enhanced datasets
sessi_data <- read_xlsx("./dataset_session.xlsx", col_names = TRUE, sheet = 1)
seme_data <- read_xlsx("./dataset_semester.xlsx", col_names = TRUE, sheet = 1)

# summary stats table
nrow(sessi_data)
sum(seme_data$Sessions_w_meals)
sum(sessi_data$Meals)

mean(seme_data$Sessions)
mean(seme_data$Sessions_w_meals)
mean(sessi_data$Meals)

median(seme_data$Sessions)
median(seme_data$Sessions_w_meals)
median(sessi_data$Meals)

min(seme_data$Sessions)
min(seme_data$Sessions_w_meals)
min(sessi_data$Meals)

max(seme_data$Sessions)
max(seme_data$Sessions_w_meals)
max(sessi_data$Meals)

sd(seme_data$Sessions)
sd(seme_data$Sessions_w_meals)
sd(sessi_data$Meals)

# model 1
summary(glm(formula=Meals ~ Transparency_all + ln_n_members + Date, family=poisson, data=sessi_data))

# model 2
summary(glm(formula=Meals ~ Transparency_all + ln_n_members + cpi_median + ep_power +
              North_Pres + Eurozone + ln_agenda + Date,family=poisson, data=sessi_data))

# model 3
summary(glm(formula=Meals ~ Transparency_all + ln_n_members + upcoming_big_close + perc_detriment + Date,
            family=poisson, data=sessi_data))

# model 4
summary(glm(formula=Meals ~ Transparency_all + ln_n_members + upcoming_big_close + perc_detriment + Date,
            family=poisson,data=sessi_data[
              !grepl("GAERC",sessi_data$Config)&
              !grepl("JHA",sessi_data$Config)&
              !grepl("EYCS",sessi_data$Config),]))

# model 5
summary(glm(formula=Meals ~ Transparency_all + ln_n_members + upcoming_big_close + perc_detriment
            + North_Pres + Eurozone + ln_agenda + Date, family=poisson, data=sessi_data))

# appendix section 1 - formal transparency
# test within model 1
summary(glm(formula=Meals ~ trans_r2 + ln_n_members + Date,family=poisson, data=sessi_data))
# test within model 2
summary(glm(formula=Meals ~ trans_r2 + ln_n_members + cpi_median + ep_power +
              North_Pres + Eurozone + ln_agenda + Date,family=poisson, data=sessi_data))
# test within model 3
summary(glm(formula=Meals ~ trans_r2 + ln_n_members + upcoming_big_close + perc_detriment + Date,
            family=poisson, data=sessi_data))
# test binary transparency variable results
summary(glm(formula=Meals ~ trans_r2_bin + Transparency_all + Date,family=poisson, data=sessi_data))
summary(glm(formula=Meals ~ trans_r2_bin + ln_n_members + Date,family=poisson, data=sessi_data))
summary(glm(formula=Meals ~ Transparency_all + ln_n_members + trans_r2_bin + Date,family=poisson, data=sessi_data))

# appendix section 2 - upcoming elections
summary(glm(formula=Meals ~ upcoming + Date,family=poisson, data=sessi_data))
summary(glm(formula=Meals ~ upcoming_big + Date,family=poisson, data=sessi_data))
summary(glm(formula=Meals ~ upcoming_close + Date,family=poisson, data=sessi_data))
summary(glm(formula=Meals ~ upcoming_big_close + Date,family=poisson, data=sessi_data))

# appendix section 4 - council formations
# configurations ordered by mean frequency
sessi_data %>%
  group_by(Config,single_mkt) %>%
  summarise(meal_freq=mean(Meals),n()) %>%
  ungroup() %>%
  filter(meal_freq>0 & meal_freq<1) %>% # filters out non-standard, mostly one-off formations
  arrange(meal_freq)

# appendix section 5 - correlation pairs
sessi_data %>% transmute(
  "Formal transparency"=Transparency_all,
  "De facto transparency"=ln_n_members,
  "Normative (CPI) transparency"=cpi_median,
  "Upcoming big close elections"=upcoming_big_close,
  "Euroskepticism"=perc_detriment
) %>%
  pairs(
    upper.panel=panel.cor,
    lower.panel=function(x,y,col=par("col"),bg=NA,pch=par("pch"),cex=1,col.smooth="black",span=2/3,iter=3, ...)
    {panel.smooth(x,y,col,bg,pch,cex,col.smooth,...)})

# appendix section 6 - transparency culture
summary(glm(formula=Meals ~ Transparency_all + ln_n_members + cpi_median + qog_trans + Date,family=poisson, data=sessi_data))

# appendix section 7 - northern council presidencies
summary(glm(formula=Meals~North_Pres+Date, family=poisson, data=sessi_data))
summary(glm(formula=Meals ~ Transparency_all + ln_n_members +
              North_Pres + ln_agenda + Date,family=poisson, data=sessi_data))

# appendix section 9 - time series considerations
# adf tests
adf.test(sessi_data$Meals)
adf.test(seme_data$Meal_freq)
# plots
acf(seme_data$Swm_freq,lag.max = length(seme_data$Swm_freq),xlab = "lag #", ylab = 'ACF',main=' ')
pacf(seme_data$Swm_freq,lag.max = length(seme_data$Swm_freq),xlab = "lag #", ylab = 'ACF',main=' ')
# differenced variable
acf(diff(seme_data$Meal_freq),lag.max = length(diff(seme_data$Meal_freq)),xlab = "lag #", ylab = 'ACF',main=' ')
# lagged dependent variable
summary(glm(formula=Meals ~ Transparency_all + ln_n_members + meals_lag,family=poisson, data=sessi_data))

# appendix section 10 - european parliament power
summary(glm(formula=Meals ~ Transparency_all + ep_power + Date,family=poisson, data=sessi_data))

# appendix section 11.1 - sessions-with-meals logistic regression
# model 1l
summary(glm(formula=swm ~ Transparency_all + ln_n_members + Date, family=binomial, data=sessi_data))
# model 2l
summary(glm(formula=swm ~ Transparency_all + ln_n_members + cpi_median +
              ep_power + North_Pres + Eurozone + ln_agenda + Date,
            family=binomial, data=sessi_data))
# model 3l
summary(glm(formula=swm ~ Transparency_all + ln_n_members + upcoming_big_close + perc_detriment + Date,
            family=binomial, data=sessi_data))
# model 4l
summary(glm(formula=swm ~ Transparency_all + ln_n_members + upcoming_big_close + perc_detriment + Date,family=binomial,
            data=sessi_data[!grepl("GAERC",sessi_data$Config) & !grepl("JHA",sessi_data$Config)& !grepl("EYCS",sessi_data$Config),]))

# appendix section 11.2 - zero-inflated
summary(zeroinfl(formula=Meals ~ Transparency_all | ln_agenda, dist='poisson', data=sessi_data))

# appendix section 12 - semester granularity
# model 1s
summary(glm(formula=Meal_freq~Transparency_all + ln_n_members + Agenda_items + sem, family=gaussian, data=seme_data))
# model 2s
summary(glm(formula=Meal_freq ~ Transparency_all + ln_n_members + cpi_median + ep_power +
              North_Pres + Agenda_items + sem,family=gaussian, data=seme_data))

# appendix section 13 - over-dispersion tests
# figures obtained in the course of running models 1-5 above

# appendix section 14 - interrupted time series
# set up time series with interaction variables
sessi_br1 <- sessi_data %>%
  filter(Transparency_all<3) %>%
  mutate(Transparency_all=Transparency_all-1) %>%
  mutate(t=as.numeric(Date)-9422, .before=Date) %>%
  mutate(interact=t*Transparency_all, .before=Date) %>%
  mutate(Transparency_all=factor(Transparency_all))
sessi_br2 <- sessi_data %>%
  filter(Transparency_all<4 & Transparency_all>1) %>%
  mutate(Transparency_all=Transparency_all-2) %>%
  mutate(t=as.numeric(Date)-11473, .before=Date) %>%
  mutate(interact=t*Transparency_all, .before=Date) %>%
  mutate(Transparency_all=factor(Transparency_all))
sessi_br3 <- sessi_data %>%
  filter(Transparency_all>2) %>%
  mutate(Transparency_all=Transparency_all-3) %>%
  mutate(t=as.numeric(Date)-14578, .before=Date) %>%
  mutate(interact=t*Transparency_all, .before=Date) %>%
  mutate(Transparency_all=factor(Transparency_all))
# test time series with interaction variables
summary(glm(formula=Meals ~ Transparency_all + log_agenda + interact, family=poisson, data=sessi_br1))
summary(glm(formula=Meals ~ Transparency_all + log_agenda + interact, family=poisson, data=sessi_br2))
summary(glm(formula=Meals ~ Transparency_all + log_agenda + interact, family=poisson, data=sessi_br3))

# appendix section 15 - single transparency variable
summary(glm(formula=Meals ~ Transparency_all + ln_n_members + Date, family=poisson, data=sessi_data))
summary(glm(formula=Meals ~ Transparency_all + Date,family=poisson, data=sessi_data))

# appendix section 16 - euro area crisis
# original crisis variable specification
sessi_data$Eurozone <- 0
sessi_data[sessi_data$Date>"2010-05-01" & sessi_data$Date<"2012-08-01","Eurozone"] <- 1
# expanded crisis variable specification
sessi_data$Eurozone <- 0
sessi_data[sessi_data$Date>"2008-09-15" & sessi_data$Date<"2013-07-01","Eurozone"] <- 1
# test within model 2
summary(glm(formula=Meals ~ Transparency_all + ln_n_members + cpi_median + ep_power +
              North_Pres + Eurozone + ln_agenda + Date,family=poisson, data=sessi_data))
# reset to original crisis variable specification
sessi_data$Eurozone <- 0
sessi_data[sessi_data$Date>"2010-05-01" & sessi_data$Date<"2012-08-01","Eurozone"] <- 1
# check
View(filter(sessi_data,Eurozone==1))


# figure 1
seme_data %>%
  mutate("Meals per session (% terms)"=Meal_freq*100) %>%
  rename("Sessions per semester"=Sessions) %>%
  pivot_longer(c("Sessions per semester","Meals per session (% terms)"),names_to = "Legend", values_to = "values") %>% #levels(.$Legend)
  ggplot(aes(sem,values,linetype=Legend)) +
  scale_linetype_discrete(breaks=c("Sessions per semester","Meals per session (% terms)")) +
  geom_line() +
  geom_smooth(method=lm,colour="black",linewidth=0.5) +
  theme(axis.title.y=element_blank(),legend.position = "bottom",legend.title=element_blank()) +
  scale_x_continuous(name="Year",breaks = seq(min(seme_data$sem), max(seme_data$sem), by = 5))

